package net.openadventure.animation;

import util.Point;
import net.openadventure.State;
import net.openadventure.sprites.Sprite;

public class FrameLoop extends Animation {

	private String spriteName;
	private String[] imagePaths;
	private int frameIndex = 0;
	private double framesPerUpdate = 1.0;
	private int updateCounter = 0;
	
	public FrameLoop(String spriteName, String[] imagePaths, double framesPerUpdate) {

		this.spriteName = spriteName;
		this.imagePaths = imagePaths;
		this.framesPerUpdate = framesPerUpdate;
	}
	
	public void updateSprite(State state) {
		Sprite target = state.getSprites().get(spriteName);

		state.getSprites().put(spriteName, new Sprite(imagePaths[frameIndex], target.getX(),
				target.getY(), target.getOriginalCenter(), target.getOriginalFootPrint(), target.getWidth(), target.getHeight(),
				target.getFlipHorizontal(), target.getFlipVertical()));
		
		updateCounter++;
		
		double numerator = framesPerUpdate * updateCounter;
		if(numerator == (int)numerator) {
			frameIndex = (frameIndex + (int)numerator) % imagePaths.length;
			updateCounter = 0;
		}
	}

	@Override
	public boolean finished(State state) {
		Sprite target = state.getSprites().get(spriteName);

		if (target != null) {
			return false;
		} else {
			return true;
		}
	}
	
	
}
